package com.teacher.hong.servlet;

import com.alibaba.fastjson.JSONObject;
import com.teacher.hong.utils.ServletUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * 获取访问量
 */
@WebServlet("/getVisits")
public class GetVisitsServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        JSONObject object = new JSONObject();
        object.put("state", 200);

        JSONObject value = new JSONObject();
        value.put("visits", ServletUtils.recordVisits(getServletContext()));

        object.put("value", value);
        object.put("timestamp", LocalDateTime.now());

        resp.setContentType("application/json");
        resp.getWriter().write(object.toJSONString());



    }
}
